package cn.monkey.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.Authentication;

import java.util.Date;

public class JwtUtil {
    private final static String miyao="hello monkey";
    public static String createToken(Authentication authentication){
        return Jwts.builder()
                .setSubject(authentication.getName())
                .setIssuedAt(new Date())
                .setIssuer("monkey")
                .setExpiration(new Date(System.currentTimeMillis()+30*60*1000))
                .signWith(SignatureAlgorithm.HS256,miyao)
                .compact();
    }

    public static Claims parseToken(String token){
        Claims body = Jwts.parser()
                .setSigningKey(miyao)
                .parseClaimsJws(token)
                .getBody();
        return body;
    }
}
